<template>
  <div id="user">
    <div class="top-back-wrapper">
      <v-top-back siteName="Vue-CN-Node社区 用户信息" ref="topBack"></v-top-back>
    </div>
    <div class="user" v-if="!error">
      <div class="user-center">
        <div class="author">
          <div class="avatar">
            <img :src="userData.avatar_url">
          </div>
          <div class="info">
            <span class="name">用户名：{{userData.loginname}}</span>
            <span class="score">积分：{{userData.score}}</span>
            <span class="create-time">注册时间：{{formatTime(userData.create_at)}}</span>
            <a :href="githubUrl" class="github">
              <svg height="20" version="1.1" viewBox="0 0 16 16" width="32">
                <path fill-rule="evenodd"
                      d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path>
              </svg>
            </a>
          </div>
        </div>
        <div class="recent">
          <div class="tab-group">
            <span class="tab" :class="{'active':activeTab === tab.value}" v-for="tab in tabs"
                  @click="checkTab(tab.value)">{{tab.title}}</span>
          </div>
          <div class="scroll-wrapper">
            <v-scroll ref="scroll"
                      v-if="recentData.length"
                      :data="recentData">
              <div class="recent-list">
                <div class="item" v-for="item in recentData">
                  <div class="avatar" @click="lookUserInfo(item.author.loginname,$event)">
                    <img v-lazy="item.author.avatar_url">
                    <span class="avatar-name">{{item.author.loginname}}</span>
                  </div>
                  <div class="item-info">
                    <div class="item-title" @click="topicDetail(item.id,$event)">{{item.title}}</div>
                    <div class="item-time">最后回复于：{{formatTime(item.last_reply_at)}}</div>
                  </div>
                </div>
              </div>
            </v-scroll>
          </div>
        </div>
      </div>
    </div>
    <div class="error-wrapper" v-if="error">
      <v-error-tip :error="error"></v-error-tip>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
  import TopBack from 'components/top-back/top-back'
  import ErrorTip from 'base/error-tip/error-tip'
  import Scroll from 'base/scroll/scroll'
  import moment from 'moment'
  import {Loading} from 'common/js/mixin'
  import {mapActions} from 'vuex'

  moment.locale('zh-cn')

  export default {
    name: 'user',
    mixins: [Loading],
    data() {
      return {
        tabs: [
          {
            'title': '最近创建的话题',
            'value': 'topics'
          },
          {
            'title': '最近参与的话题',
            'value': 'replies'
          }
        ],
        username: this.$route.params.username,
        userData: '',
        error: '',
        activeTab: 'topics',
        recentData: []
      }
    },
    created() {
      this._getUserInfo()
    },
    mounted() {
      this.$nextTick(() => {
        if (this.$refs.scroll) {
          this.$refs.scroll.refresh()
        }
      })
    },
    computed: {
      githubUrl() {
        return `https://github.com/${this.userData.githubUsername}`
      }
    },
    methods: {
      checkTab(tab) {
        this.activeTab = tab
        this.recentData = this.userData[`recent_${this.activeTab}`]
      },
      // 查看话题详情
      topicDetail(id, event) {
        if (!event._constructed) {
          return
        }
        this.$router.push({name: 'topic-detail', params: {id: id}})
      },
      // 查看用户信息
      lookUserInfo(loginname, event) {
        if (!event._constructed) {
          return
        }
        this.$router.push({name: 'user', params: {username: loginname}})
      },
      formatTime(time) {
        return moment(time).startOf().fromNow()
      },
      _getUserInfo() {
        this.showLoading({})
        return this.getSelfInfo({
          loginname: this.username
        }).then((res) => {
          this.closeLoading()
          if (res.success === true) {
            this.userData = res.data
            this.recentData = this.userData[`recent_${this.activeTab}`]
            this.error = ''
            return true
          } else {
            this.error = res.error_msg
            return false
          }
        })
      },
      ...mapActions([
        'getSelfInfo'
      ])
    },
    activated() {
      if (this.username !== this.$route.params.username) {
        this.username = this.$route.params.username
      }
    },
    watch: {
      username() {
        this._getUserInfo()
      },
      '$route'(to, from) {
        if (to.name === from.name) {
          this.activeTab = 'topics'
          this.username = this.$route.params.username
        }
      }
    },
    components: {
      'v-top-back': TopBack,
      'v-error-tip': ErrorTip,
      'v-scroll': Scroll
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss">
  @import "~common/scss/const";
  @import "~common/scss/mixin";

  #user {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    width: 100%;
    overflow: hidden;
    .user {
      position: absolute;
      top: 45px;
      bottom: 0;
      left: 0;
      right: 0;
      background-color: #eff2f7;
      .user-center {
        padding: 5px;
        box-sizing: border-box;
        width: 100%;
        height: 100%;
        display: flex;
        flex-direction: column;
        > div {
          background-color: $color-background;
          box-sizing: border-box;
          padding: 10px;
          display: flex;
          position: relative;
          margin-top: 5px;
          &:first-child {
            margin-top: 0;
          }
          &.author {
            flex: 0 0 100px;
            height: 100px;
            .avatar {
              flex: 0 0 80px;
              width: 80px;
              overflow: hidden;
              img {
                display: inline-block;
                width: 100%;
                border-radius: 5px;
              }
            }
            .info {
              flex: 1;
              display: flex;
              flex-direction: column;
              justify-content: space-between;
              white-space: nowrap;
              overflow: hidden;
              padding: 5px 0;
              margin-left: 10px;
              box-sizing: border-box;
              .name {
                font-size: $font-size-medium;
                color: $color-text-bb;
                font-weight: bold;
              }
              .score {
                font-size: $font-size-medium;
                color: $color-text-bb;
              }
              .create-time {
                font-size: $font-size-small;
                color: #999999;
              }
              .github {
                display: inline-block;
                position: absolute;
                right: 0;
                top: 0;
                padding: 12px 7px;
              }
            }
          }
          &.recent {
            flex: 1;
            padding: 0;
            .tab-group {
              width: 100%;
              height: 34px;
              display: flex;
              border-bottom: 1px solid #dddddd;
              .tab {
                display: inline-block;
                flex: 1;
                text-align: center;
                line-height: 34px;
                font-size: $font-size-medium;
                border-left: 1px solid #dddddd;
                color: #666666;
                cursor: pointer;
                &:first-child {
                  border-left: none;
                }
                &.active {
                  color: #000000;
                }
              }
            }
            .scroll-wrapper {
              position: absolute;
              top: 35px;
              bottom: 0;
              right: 0;
              left: 0;
              overflow: hidden;
              width: 100%;
              .recent-list {
                width: 100%;
                overflow: hidden;
                box-sizing: border-box;
                padding: 5px;
                .item {
                  background-color: $color-background;
                  margin-top: 10px;
                  padding: 5px;
                  box-sizing: border-box;
                  box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);
                  display: flex;
                  &:first-child {
                    margin-top: 5px;
                  }
                  .avatar {
                    flex: 0 0 70px;
                    width: 70px;
                    overflow: hidden;
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                    justify-content: space-around;
                    cursor: pointer;
                    img {
                      display: inline-block;
                      flex: 0 0 40px;
                      width: 40px;
                      border-radius: 50%;
                    }
                    .avatar-name {
                      display: inline-block;
                      flex: 0 0 20px;
                      width: 100%;
                      height: 20px;
                      text-align: center;
                      font-size: $font-size-small;
                      color: #999999;
                      line-height: 20px;
                      margin-top: 10px;
                      @include no-wrap;
                    }
                  }
                  .item-info {
                    flex: 1;
                    margin-left: 10px;
                    display: flex;
                    flex-direction: column;
                    justify-content: space-around;
                    .item-title {
                      line-height: 20px;
                      flex: 0 0 40px;
                      height: 40px;
                      overflow: hidden;
                      color: $color-text-bb;
                      font-size: $font-size-medium;
                      cursor: pointer;
                      &:hover {
                        color: $color-active;
                      }
                    }
                    .item-time {
                      overflow: hidden;
                      height: 20px;
                      line-height: 20px;
                      white-space: nowrap;
                      color: #999999;
                      font-size: $font-size-small;
                      margin-top: 10px;
                    }
                  }
                }
              }

            }
          }
        }
      }
    }
  }
</style>
